home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / src / scancnvx.c < prev    next >
Text File  |  1993-12-06  |  4KB  |  149 lines

  1. /**
  2.  ** SCANCNVX.C
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #include "grx.h"
  25. #include "libgrx.h"
  26. #include "clipping.h"
  27. #include "scale.h"
  28.  
  29. void _GrScanConvexPoly(int n,int pt[][2],
  30.     int is_XOR_color,
  31.     _GrPixelDrawProc pixelproc,
  32.     _GrLineDrawProc  borderproc,
  33.     _GrScanLineProc  scanfillproc,
  34.     void *fillarg)
  35. {
  36.     int  *lp1,*lp2,*rp1,*rp2,*last;
  37.     int  lxx,ldx,ldy,lslope,lerror;
  38.     int  rxx,rdx,rdy,rslope,rerror;
  39.     int  ymin,ymax,ynext;
  40.     MOUSE_FLAG;
  41.  
  42.     if((n > 1) && (pt[0][0] == pt[n-1][0]) && (pt[0][1] == pt[n-1][1])) n--;
  43.     if(n <= 2) {
  44.         if(n <= 0) return;
  45.         ldx = pt[0][0];
  46.         ldy = pt[0][1];
  47.         if(n == 1) {
  48.         MOUSE_BLOCK(CURC,ldx,ldy,ldx,ldy);
  49.         (*pixelproc)(ldx,ldy,fillarg);
  50.         }
  51.         else {
  52.         rdx = pt[1][0];
  53.         rdy = pt[1][0];
  54.         MOUSE_BLOCK(CURC,ldx,ldy,rdx,rdy);
  55.         (*borderproc)(ldx,ldy,rdx,rdy,fillarg);
  56.         }
  57.         MOUSE_UNBLOCK();
  58.         return;
  59.     }
  60.     lxx = ymin = 32000;
  61.     rxx = ymax = (-32000);
  62.     for(last = pt[0]; --n >= 0; last += 2) {
  63.         if(lxx  > last[0]) lxx  = last[0];
  64.         if(rxx  < last[0]) rxx  = last[0];
  65.         if(ymin > last[1]) ymin = (lp2 = rp2 = last)[1];
  66.         if(ymax < last[1]) ymax = last[1];
  67.     }
  68.     if(ymin == ymax) {
  69.         CLIPHLINE(CURC,lxx,rxx,ymin);
  70.         MOUSE_BLOCK(CURC,lxx,ymin,rxx,ymin);
  71.         (*scanfillproc)(lxx,rxx,ymin,fillarg);
  72.         MOUSE_UNBLOCK();
  73.         return;
  74.     }
  75.     CLIPSORTEDBOX(CURC,lxx,ymin,rxx,ymax);
  76.     MOUSE_BLOCK(CURC,lxx,ymin,rxx,ymax);
  77.     last -= 2;
  78.     for( ; ; ) {
  79.         lp1 = lp2;
  80.         if((lp2 -= 2) < pt[0]) lp2 = last;
  81.         if((lp1[1] != lp2[1]) && (lp2[1] >= _GrLoY)) {
  82.         lxx = lp1[0];
  83.         ldy = lp2[1] - lp1[1];
  84.         if(lp1[1] < _GrLoY) {
  85.             SCALE(ldx,(lxx - lp2[0]),(lp2[1] - _GrLoY),ldy);
  86.             lxx = lp2[0] + ldx;
  87.             ldy = lp2[1] - _GrLoY;
  88.         }
  89.         break;
  90.         }
  91.     }
  92.     for( ; ; ) {
  93.         rp1 = rp2;
  94.         if((rp2 += 2) > last) rp2 = pt[0];
  95.         if((rp1[1] != rp2[1]) && (rp2[1] >= _GrLoY)) {
  96.         rxx = rp1[0];
  97.         rdy = rp2[1] - rp1[1];
  98.         if(rp1[1] < _GrLoY) {
  99.             SCALE(rdx,(rxx - rp2[0]),(rp2[1] - _GrLoY),rdy);
  100.             rxx = rp2[0] + rdx;
  101.             rdy = rp2[1] - _GrLoY;
  102.         }
  103.         break;
  104.         }
  105.     }
  106.     for( ; ; ) {
  107.         if(ldy > 0) {
  108.         lslope = (ldx = lp2[0] - lxx) / ldy;
  109.         if(lslope && !is_XOR_color) (*borderproc)(lp1[0],lp1[1],lp2[0],lp2[1],fillarg);
  110.         if((ldx %= ldy) < 0) { lslope--; ldx += ldy; }
  111.         lerror = (ldy >> 1);
  112.         ldy = (-ldy);
  113.         }
  114.         if(rdy > 0) {
  115.         rslope = (rdx = rp2[0] - rxx) / rdy;
  116.         if(rslope && !is_XOR_color) (*borderproc)(rp1[0],rp1[1],rp2[0],rp2[1],fillarg);
  117.         if((rdx %= rdy) < 0) { rslope--; rdx += rdy; }
  118.         rerror = (rdy >> 1);
  119.         rdy = (-rdy);
  120.         }
  121.         ynext = lp2[1];
  122.         if(ynext >= rp2[1]) ynext = rp2[1];
  123.         if(ynext >= ymax)    ynext = ymax + 1;
  124.         while(ymin < ynext) {
  125.         (*scanfillproc)(lxx,rxx,ymin,fillarg);
  126.         if((lerror -= ldx) < 0) lxx++,lerror -= ldy;
  127.         if((rerror -= rdx) < 0) rxx++,rerror -= rdy;
  128.         lxx += lslope;
  129.         rxx += rslope;
  130.         ymin++;
  131.         }
  132.         if(ymin >= ymax) break;
  133.         while(ynext == lp2[1]) {
  134.         lp1 = lp2;
  135.         if((lp2 -= 2) < pt[0]) lp2 = last;
  136.         lxx = lp1[0];
  137.         ldy = lp2[1] - lp1[1];
  138.         }
  139.         while(ynext == rp2[1]) {
  140.         rp1 = rp2;
  141.         if((rp2 += 2) > last) rp2 = pt[0];
  142.         rxx = rp1[0];
  143.         rdy = rp2[1] - rp1[1];
  144.         }
  145.     }
  146.     MOUSE_UNBLOCK();
  147. }
  148.  
  149.